* ======================================
* ---------- Split Immigrants ----------
* ======================================


	use $dta\EstimationSample.dta, clear

	
* Split by origin and bloc
* ========================

	* Voter's origin
	gen Voter_nor = 1*(Voter_birthcountry == "000")
	gen Voter_imm = 1*(Voter_immigrant == 1)
	

* Estimate models
* ===============	
	
	egen idbsu = group(Voter_id Voter_bsu)
	gen period = 1*(year == 2019)
	xtset idbsu period
	estimates clear	

	foreach var in Voter_nor Voter_imm {
	foreach net in Fam1 Fam2 {			
		preserve
	
		keep if `var' == 1
		
		sum Voter_turnout 	// mean depvar
		local meandepvar = string(r(mean)*100, "%15.2fc")
	
		gen Any = 1*(`net'Any == 1)
		gen Same = 1*(`net'Same == 1)
		eststo NatImm_`var'_`net': xtreg Voter_turnout Any Same i.period, ///
			i(idbsu) fe cluster(Voter_bsu)
		quietly estadd local meandep `meandepvar', replace
	
		restore
		}
	}
	
* Export table
* ============

	esttab NatImm_* ///
		using $tbls\NativesImmigrants.tex, replace  style(tex) ///
		prehead("\begin{tabular}{l*{5}{c}} \hline\hline \\  ") ///
		posthead("\hline \\") ///
		fragment ///
		mtitles("Close" "Extended" "Close" "Extended") ///
		mgroups("\shortstack{\\ Natives}" "\shortstack{\\ \\ Immigrants}" ///
			, pattern(1 0 1 0) ///
			span prefix(\multicolumn{@span}{c}{) suffix(}) ///
			end(\cmidrule(lr){2-3} \cmidrule(lr){4-5})) ///
		label se b(%9.3f) se(%9.3f) nostar ///
		refcat(Any "\shortstack{ No candidate in network }") ///
		coeflabels(Any "Any District" Same "Same District" ) ///
		drop(_cons *.period) noomitted nobaselevels ///
		nonotes stats(N N_clust meandep, labels( ///
			"Observations" "Clusters" "Mean turnout (\%)") ///
			fmt(%15.0fc %15.0fc %15.0fc)) ///
			substitute(\_ _) ///
		prefoot("\hline") ///
		postfoot("\hline\hline \\ \end{tabular}")
